﻿using Microsoft.AspNetCore.Mvc;
using Shop.Core.Share;
using Shop.IServices;
using Shop.Model.Dto.Goo;
using Shop.Model.Entity.Goo;
using Shop.Model.View.Goo;

namespace Shop.WebApi.Controllers;

/// <summary>
/// 商品
/// </summary>
[ApiController]
[Route("Api/[controller]")]
public class GoodsController:ShopControllerBase
{
    private readonly IGoodsAppService _goodsAppService;
    private readonly IUserContext _userContext;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="goodsAppService"></param>
    /// <param name="userContext"></param>
    public GoodsController(IGoodsAppService goodsAppService,IUserContext userContext)
    {
        _goodsAppService = goodsAppService;
        _userContext = userContext;
    }

    /// <summary>
    /// 礼包列表
    /// </summary>
    /// <param name="dto">语言</param>
    /// <returns></returns>
    [HttpGet]
    [Route("GiftBag")]
    public AppResult<PageModel<GiftBagVo>> GiftBag([FromQuery]GiftBagDto dto)
    {
        return _goodsAppService.GiftBag(_userContext.Id, dto);
    }
    
    /// <summary>
    /// 首页新品
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("NewGoo")]
    public AppResult<NewGoodsVo> NewGoo([FromQuery]string language)
    {
        return _goodsAppService.NewGoo(_userContext.Id, language);
    }

    /// <summary>
    /// 商品列表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("GooList")]
    public async Task<AppResult<PageModel<GooListVo>>> GooList([FromQuery]GooListDto dto)
    {
        return await _goodsAppService.GooList(_userContext.Id,dto);
    }

    /// <summary>
    /// 商品详情
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("Particulars")]
    public AppResult<GooParticularsVo> Particulars([FromQuery]ParticularsDto dto)
    {
        return _goodsAppService.Particulars(_userContext.Id,dto);
    }

    /// <summary>
    /// 商品评论
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("Review")]
    public AppResult<PageModel<ReviewVo>> Review([FromQuery] ReviewDto dto)
    {
        return _goodsAppService.Review(dto);
    }

    /// <summary>
    /// 商品运费
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("GoodsDelivery")]
    public async Task<AppResult<CalculateDeliveryCostVo>> CalculateDeliveryCost(CalculateDeliveryCostDto dto)
    {
        try
        {
            return await _goodsAppService.CalculateDeliveryCost(dto);
        }
        catch (Exception ex)
        {
            return Problem(ex.Message.ToString());
        }
        
    }
    
    /// <summary>
    /// 商品赠送规则
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpGet("GiftRules")]
    public AppResult<GiftRulesVo> GiftRules([FromQuery] GiftRulesDto dto)
        => _goodsAppService.GiftRules(dto);
}